--[[
卡牌显示对象
]]
local CardViewBehavior = class("CardViewBehavior", BehaviorBase)

function CardViewBehavior:ctor()
    CardViewBehavior.super.ctor(self, "CardViewBehavior", nil, 0)
end


    
    
function CardViewBehavior:bind(object)
	
	
	
	--[[
	通过id 获取当前卡牌的 x，y索引
	]]
	local function getXYIndexById(object)
	    local id = object.id_ ;
	    local str = string.split(id, ":")[2];
	    local index = string.split(str, "_");
	    return index[1],index[2];
    end
    object:bindMethod(self, "getXYIndexById", getXYIndexById) 
    
    
    
	--卡片更改id
    local function changeId(object,newId)
    	local classId = object.classId_;
    	object.model_.objects_[newId] = object
	    if not object.model_.objectsByClass_[classId] then
	        object.model_.objectsByClass_[classId] = {}
	    end
	    object.model_.objectsByClass_[classId][newId] = object
	    object.id_ = newId;
	    object:updateView();
    end
    object:bindMethod(self, "changeId", changeId) 


	
    --卡片移动
    local function moveTo(object,moveToX,moveToY,onComplete,time)
    	local spt = object:getView();
    	local onCompleteHandle = function()
    		if onComplete then onComplete() end
    		object:setPosition(moveToX,moveToY);
    	end
    	
    	local batch = object.batch_;
    	batch:reorderChild(spt,SceneConstants.MAX_OBJECT_ZORDER -1)
    	transition.moveTo(spt, {
    		x = moveToX, y = moveToY,
    		time = time or 0.3,
    		easing = "sineInOut",
    		onComplete = onCompleteHandle
    	})
	 	  				
    end
    object:bindMethod(self, "moveTo", moveTo) 
    
    
    
    
    
     local function fadeIn(object,onComplete,time)
    	local spt = object:getView();
    	local onCompleteHandle = function()
    		if onComplete then onComplete() end
    	end
    	transition.fadeIn(spt, {
    		time = time or 0.3,
    		easing = "sineInOut",
    		onComplete = onCompleteHandle
    	})		
    end
    object:bindMethod(self, "fadeIn", fadeIn)
    
    local function fadeOut(object,onComplete,time)
    	local spt = object:getView();
    	local onCompleteHandle = function()
    		if onComplete then onComplete() end
    	end
    	transition.fadeOut(spt, {
    		time = time or 0.3,
    		easing = "sineInOut",
    		onComplete = onCompleteHandle
    	})		
    end
    object:bindMethod(self, "fadeOut", fadeOut) 
    
    
    
    --更改图片的 cardSpriteId 
    local function setCardSpriteId(object,cardSpriteId)
    	object.previousCardSpriteId_ = object.cardSpriteId_;
    	
    	local animationCache= object:getAnimation();
    	object.cardSpriteId_ = cardSpriteId or 0 ;
    	
    	object.imageName_ = "cardSprite_"..cardSpriteId.."_0.png" or "cardSprite_1_0.png";
    	animationCache:updataSpriteByImageName(object.imageName_);
    	
    	
    	--是否是障碍物	锁住不能移动
		--isObstacle	movingLocked
    	
    end
    object:bindMethod(self, "setCardSpriteId", setCardSpriteId) 
    
    
    
    --获取图片的 caridId
    local function getCardSpriteId(object)
    	return object.cardSpriteId_ or 0 ;
    end
    object:bindMethod(self, "getCardSpriteId", getCardSpriteId) 
    --获取上一次图片的 caridId
    local function getPreviousCardSpriteId(object)
    	return object.previousCardSpriteId_ or 0 ;
    end
    object:bindMethod(self, "getPreviousCardSpriteId", getPreviousCardSpriteId) 
end

function CardViewBehavior:unbind(object)
    object:unbindMethod(self, "createView")
end

return CardViewBehavior
